package controllers

import (
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/services"
	"weiboxs.com/new_h_glasses/utils"
)

type StatisticsController struct {
	BaseController
}

func (c *StatisticsController) Prepare() {
	c.BaseController.Prepare()

}

//销售汇总
func (c *StatisticsController) Retail() {
	c.checkAuthor("statistics/retail")
	c.Data["activeSidebarUrl"] = "/statistics/retail"

	Q := services.QueryParamsStatistics{}
	if c.Ctx.Input.IsPost() {
		Q.DepartmentId = c.curUser.DepartmentId
		query_type, _ := c.GetInt("query_type", 0)
		var (
			start_date string
			end_date   string
		)
		switch query_type {
		case 1: //本周
			start_date, end_date = utils.GetWeekDay()
			break
		case 2: //本月
			start_date, end_date = utils.GetMonthDay()
			break
		case 3: //上月
			start_date, end_date = utils.GetLastMonthDay()
			break
		case 5: //自定日期
			start_date = c.GetString("start_date", "")+ " 00:00"
			end_date = c.GetString("end_date", "")+ " 23:59"
			break
		}
		Q.StartDate = start_date
		Q.EndDate = end_date
		if data, err := Q.Retail(); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", data)
		}
	}

	c.Data["btn"] = "retail"
	c.display("", "statistics/retail_footerjs.html")
}

//销售明细
func (c *StatisticsController) RetailDetailed() {
	c.checkAuthor("statistics/retail")
	Q := services.QueryParamsStatistics{}
	if c.Ctx.Input.IsPost() {
		Q.DepartmentId = c.curUser.DepartmentId
		query_type, _ := c.GetInt("query_type", 0)
		var (
			start_date string
			end_date   string
		)
		switch query_type {
		case 1: //本周
			start_date, end_date = utils.GetWeekDay()
			break
		case 2: //本月
			start_date, end_date = utils.GetMonthDay()
			break
		case 3: //上月
			start_date, end_date = utils.GetLastMonthDay()
			break
		case 5: //自定日期
			start_date = c.GetString("start_date", "")+ " 00:00"
			end_date = c.GetString("end_date", "")+ " 23:59"
			break
		}
		Q.StartDate = start_date
		Q.EndDate = end_date
		if data, err := Q.RetailDetailed(); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", data)
		}
	}
	c.Data["activeSidebarUrl"] = "/statistics/retail"
	c.Data["btn"] = "retail_detailed"

	c.display("", "statistics/retaildetailed_footerjs.html")

}

//出入库汇总
func (c *StatisticsController) Entry() {
	c.checkAuthor("statistics/entry")
	Q := services.QueryParamsStatistics{}
	if c.Ctx.Input.IsPost() {
		Q.DepartmentId = c.curUser.DepartmentId
		query_type, _ := c.GetInt("query_type", 0)
		var (
			start_date string
			end_date   string
		)
		switch query_type {
		case 1: //本周
			start_date, end_date = utils.GetWeekDay()
			break
		case 2: //本月
			start_date, end_date = utils.GetMonthDay()
			break
		case 3: //上月
			start_date, end_date = utils.GetLastMonthDay()
			break
		case 5: //自定日期
			start_date = c.GetString("start_date", "")+ " 00:00"
			end_date = c.GetString("end_date", "")+ " 23:59"
			break
		}
		Q.StartDate = start_date
		Q.EndDate = end_date
		if data, err := Q.Entry(); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", data)
		}
	}

	c.Data["activeSidebarUrl"] = "/statistics/entry"

	c.Data["btn"] = "entry"

	c.display("", "statistics/entry_footerjs.html")
}

//出入库明细
func (c *StatisticsController) EntryDetailed() {
	c.checkAuthor("statistics/entry")
	Q := services.QueryParamsStatistics{}
	if c.Ctx.Input.IsPost() {
		Q.DepartmentId = c.curUser.DepartmentId
		query_type, _ := c.GetInt("query_type", 0)
		var (
			start_date string
			end_date   string
		)
		switch query_type {
		case 1: //本周
			start_date, end_date = utils.GetWeekDay()
			break
		case 2: //本月
			start_date, end_date = utils.GetMonthDay()
			break
		case 3: //上月
			start_date, end_date = utils.GetLastMonthDay()
			break
		case 5: //自定日期
			start_date = c.GetString("start_date", "")+ " 00:00"
			end_date = c.GetString("end_date", "")+ " 23:59"
			break
		}
		Q.StartDate = start_date
		Q.EndDate = end_date
		if data, err := Q.EntryDetailed(); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", data)
		}
	}
	c.Data["activeSidebarUrl"] = "/statistics/entry"
	c.Data["btn"] = "entry_detailed"

	c.display("", "statistics/entrydetailed_footerjs.html")
}

//运营报表
func (c *StatisticsController) Operate() {
	c.checkAuthor("statistics/operate")
	if c.Ctx.Input.IsPost() {
		query_type, _ := c.GetInt("query_type", 0)
		var (
			start_date string
			end_date   string
		)
		switch query_type {
		case 1: //本周
			start_date, end_date = utils.GetWeekDay()
			break
		case 2: //本月
			start_date, end_date = utils.GetMonthDay()
			break
		case 3: //上月
			start_date, end_date = utils.GetLastMonthDay()
			break
		case 5: //自定日期
			start_date = c.GetString("start_date", "")+ " 00:00"
			end_date = c.GetString("end_date", "")+ " 23:59"
			break
		}
		Q := services.QueryParamsStatistics{}
		Q.StartDate = start_date
		Q.EndDate = end_date
		Q.DepartmentId = c.curUser.DepartmentId
		if data, err := Q.Operate(); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", data)
		}
	}
	c.Data["activeSidebarUrl"] = "/statistics/operate"
	c.showErpTypeid()
	c.display("", "statistics/operate_footerjs.html")
}

//门诊统计
func (c *StatisticsController) Presc() {
	c.checkAuthor("statistics/presc")
	if c.Ctx.Input.IsPost() {
		query_type, _ := c.GetInt("query_type", 0)
		var (
			start_date string
			end_date   string
		)
		switch query_type {
		case 1: //本周
			start_date, end_date = utils.GetWeekDay()
			break
		case 2: //本月
			start_date, end_date = utils.GetMonthDay()
			break
		case 3: //上月
			start_date, end_date = utils.GetLastMonthDay()
			break
		case 5: //自定日期
			start_date = c.GetString("start_date", "")+ " 00:00"
			end_date = c.GetString("end_date", "")+ " 23:59"
			break
		}
		var params models.SaCollectfeesDoctororderMiddleQueryParam
		params.DepartmentId = c.curUser.DepartmentId
		params.StartDate = start_date
		params.EndDate = end_date
		data:= models.SaCollectfeesDoctororderMiddleList(&params)
		c.jsonResult(enums.JRCodeSucc, "", data)
	}
	c.Data["activeSidebarUrl"] = "/statistics/presc"
	c.display("", "statistics/presc_footerjs.html")
}

//库存汇总
func (c *StatisticsController) Stock() {

	c.checkAuthor("statistics/stock")
	if c.Ctx.Input.IsPost() {
		Q := services.QueryParamsStatistics{}
		Q.DepartmentId = c.curUser.DepartmentId
		suppliers_id,_ := c.GetInt64("suppliers")
		product_name := c.GetString("product_name")
		if data, err := Q.Stock(suppliers_id, product_name); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "ok", data)
		}
	}
	suppliers := models.ErpSuppliersList(&models.ErpSuppliersQueryParam{DepartmentId: c.curUser.DepartmentId})
	c.Data["suppliers"] = suppliers

	c.Data["activeSidebarUrl"] = "/statistics/stock"
	c.display("", "statistics/stock_footerjs.html")
}
